﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using Geosoft.GXNet;
using Geosoft.GX.Controls;
namespace $NAMESPACE$
{
   public partial class $CLASSNAME$ : BaseForm
   {
      #region Member Variables
      #endregion
      #region Constructors/Destructors
      public $CLASSNAME$()
      {
         // This call is required by the Windows.Forms Form Designer.
         InitializeComponent();

      }

      public $CLASSNAME$(int i)
         : base(i)
      {
         InitializeComponent();

      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose(bool disposing)
      {
         if (disposing)
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose(disposing);
      }
      #endregion
      // The overrided methods in the region below does any extra work needed during the various 
      //       parts of the GX flow (initialization, validation etc.) which cannot be provided by the controls 
      //       alone. Some controls will validate themselves without having to add any extra code here and where
      //       possible that functionality should be used without having to add any code here (e.g. the Required 
      //       property, or the MaximumValue on the EditIntControl).
      //
      // NOTE: Do not use a message box in these overrides if you intend to use the GX in the non-interactive
      //       scripting environment. The use of CSYS.Abort, CSYS.DisplayMessage etc. are preferred.

      #region BaseForm Overrides
      /// <summary>
      /// Override to do extra initialization (combobox lst init etc.)
      /// </summary>
      /// <remarks>The derived class should call this function in the appropriate place</remarks>
      protected override void InitializeForm()
      {
         base.InitializeForm();

         // TODO: If needed add extra initialization/pre-validation here, for example:
         //
         //
         // CEDB hEDB = null;
         // CDB hDB = null;
         // try
         // {
         //    hEDB = CEDB.Current();
         //    hDB = hEDB.Lock();
         //    CLST hChanLST = CLST.Create(128);
         //    hDB.ArrayLST(hChanLST);
         //
         //    if (hChanLST.iSize() < 2)
         //       CSYS.Abort("At least two array channels are required.");
         //    m_cmbData.Init(hChanLST, Constant.LST_ITEM_NAME);
         // }
         // finally
         // {
         //    if (hDB == null) 
         //       hEDB.UnLock();
         // }
      }

      /// <summary>
      /// Override to do extra validation
      /// </summary>
      /// <param name="strINIError">
      /// </param>
      /// <param name="bUseErrorProvider">
      /// </param>
      /// <returns>true if validation is successful</returns>
      public override bool bValidateDialog(ref string strINIError, bool bUseErrorProvider)
      {
         bool bValid = true;

         // Do base dialog validation first (this will validate all controls based on their properties first)

         if (!base.bValidateDialog(ref strINIError, bUseErrorProvider))
            bValid = false;

         if (bValid)
         {
            // TODO: Add any more complex validation logic here. Not that 3 things need to happen for validation failures:
            //
            //       1. bValid should be set to false.
            //       2. Create a string describing the problem and if the bUseErrorProvider parameter is true set this
            //          error and the control in question to the base class' ControlErrorProvider.SetError method.
            //       3. Append an error string to the strINIError string parameter decribing the problem. The convenient 
            //          FormatINIErrorString of the GX Controls may be used if the problem lies in a specific control,
            //          This ensures logical errors are reported during scripting of GXs.
            //       
            //       For Example:
            //
            // 
            // int iNGrids = m_EditNumGrids.iGetValue();
            // double dStartElev = m_EditStartElev.dGetValue();
            // double dElevInc = m_EditElevInc.dGetValue();
            // 
            // if (iNGrids != Constant.iDUMMY && dStartElev == Constant.rDUMMY)
            // {
            //    bValid = false;
            //    strError = "The starting elevation must be defined if the number of grids is defined.";
            //    if (bUseErrorProvider)
            //       this.ControlErrorProvider.SetError(m_EditStartElev, strError);
            //    strINIError += m_EditStartElev.FormatINIErrorString(strError);
            // }
            // if (iNGrids > 1 && dElevInc == Constant.rDUMMY)
            // {
            //    bValid = false;
            //    strError = "The elevation increment must be defined if the number of grids is defined and is greater 1.";
            //    if (bUseErrorProvider)
            //       this.ControlErrorProvider.SetError(m_EditElevInc, strError);
            //    strINIError += m_EditElevInc.FormatINIErrorString(strError);
            // }
            // else if (dElevInc == 0.0 && iNGrids > 1)
            // {
            //    bValid = false;
            //    strError = "The elevation increment must not be zero if the number of grids is defined and is greater 1.";
            //    if (bUseErrorProvider)
            //       this.ControlErrorProvider.SetError(m_EditElevInc, strError);
            //    strINIError += m_EditElevInc.FormatINIErrorString(strError);
            // }
         }
         return bValid;
      }

      #endregion
      // TODO: This region contains the first method called by montaj after instantiating the GX.
      //       In general the code inside may be left as is.
      #region Entry Point Method
      /// <summary>
      /// Run the GX.
      /// </summary>
      [CGXAttribute("")]
      public void Run()
      {
         DialogResult Result = DialogResult.OK;

         try
         {
            // Initialize the form
            InitializeForm();

            // Are we running interactively?

            if (CSYS.iInteractive() != 0)
               Result = this.ShowDialog();
         }
         catch (ApplicationException e)
         {
            // An empty message indicates a CSYS.Cancel()
            if (e.Message.Length > 0)
               CSYS.Abort(e.Message);
            else
               Result = DialogResult.Cancel;
         }
         catch (Exception e)
         {
            CSYS.Abort("Failure in the \"" + this.Name + "\" GX.NET Form (" + e.Message + ")");
         }

         if (Result == DialogResult.OK)
         {
            try
            {
               Execute();
            }
            catch (Exception e)
            {
               // An empty message indicates a cancel
               if (e.Message.Length > 0)
                  CSYS.Abort(e.Message);
            }
         }
      }
      #endregion Entry Point
      // TODO: Organize any event handlers added (either manually or through the designer) in the region below. Event
      //       handlers may be used to provide interactive changes to the form appearance based on user input which
      //       can deliver a more natural user experience (e.g. enabling/disabling controls based on reaction 
      //       to a SelectedValueChanged event from a ComboBoxControl).
      //
      //       Note: The default behaviour for the OK button has already been added. None is needed for the Cancel button
      //       in most cases.
      //
      //       Should any extra logic be needed it is generally a good idea to add it to the BaseForm overrides above 
      //       rather than inside m_btnOK_Click which will make things easier to maintain as well as maintain the ability 
      //       of the GX to run in non-interactive (scripting) mode and function logically.
      #region Event Handlers
      private void m_btnOK_Click(object sender, EventArgs e)
      {
         try
         {
            if (!bValidateDialog())
            {
               // This to not close the dialog
               DialogResult = DialogResult.None;
               return;
            }
         }
         catch (Exception ex)
         {
            // Show errors and clear it so that it will not present again on GX exit
            if (ex.Message.Length > 0)
               MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            CSYS.iClearErrAP();
            // This to not close the dialog
            DialogResult = DialogResult.None;
         }
      }
      #endregion Event Handlers
       
      private void Execute()
      {
 